package client.interactor;

import java.util.Date;

import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteExtension;
import wiiremotej.WiiRemoteJ;
import wiiremotej.event.WRAccelerationEvent;
import wiiremotej.event.WRButtonEvent;
import wiiremotej.event.WRCombinedEvent;
import wiiremotej.event.WRExtensionEvent;
import wiiremotej.event.WRIREvent;
import wiiremotej.event.WRNunchukExtensionEvent;
import wiiremotej.event.WRStatusEvent;
import wiiremotej.event.WiiRemoteListener;

//Classe permettant l'utilisation de la wiimote et du nunchuck pour le deplacement
public class WiimoteInteractorBehavior extends InteractorBehavior {
	
	private WiiRemote wiimote;
	private Long precedentTime;
	private boolean buttonCPressed;
	private boolean buttonZPressed;
	
	public WiimoteInteractorBehavior(Interactor i, PickerBehavior p) {
		super(i, p);
		
		precedentTime = (new Date()).getTime();
		buttonCPressed = false;
		buttonZPressed = false;
		
		wiimote = null ;
		
		try {
			wiimote = WiiRemoteJ.findRemote();
			System.out.println("adress wiimote: " + wiimote.getBluetoothAddress());
			//wiimote = WiiRemoteJ.connectToRemote ("0022A9D9EDDB") ;
			wiimote.addWiiRemoteListener (new WiimoteListener ()) ;
			wiimote.setIRSensorEnabled (true, WRIREvent.BASIC) ;
			//wiimote.setAccelerometerEnabled (true) ;
     	} catch (Exception e) {
	  		//e.printStackTrace () ;
  		}
	}
	
	class WiimoteListener implements WiiRemoteListener {

		public void IRInputReceived(WRIREvent arg0) {
			
		}

		public void accelerationInputReceived(WRAccelerationEvent arg0) {
			
		}

		public void buttonInputReceived(WRButtonEvent be) {
			
			if (be.wasPressed (WRButtonEvent.TWO)) System.out.println ("2 pressed") ;
	        if (be.wasPressed (WRButtonEvent.ONE)) System.out.println ("1 pressed") ;
	        if (be.wasPressed (WRButtonEvent.B)) System.out.println ("B pressed") ;
	        if (be.wasPressed (WRButtonEvent.A)) System.out.println ("A pressed") ;
	        if (be.wasPressed (WRButtonEvent.MINUS)) System.out.println ("Minus pressed") ;
	        if (be.wasPressed (WRButtonEvent.HOME)) System.out.println ("Home pressed") ;
	        if (be.wasPressed (WRButtonEvent.LEFT)) {
	        	
	        }
	        if (be.wasPressed (WRButtonEvent.RIGHT)) {
	        	
	        }
	        if (be.wasPressed (WRButtonEvent.DOWN)) {
	        	
	        }
	        if (be.wasPressed (WRButtonEvent.UP)) {
	        	
	        }
	        if (be.wasPressed (WRButtonEvent.PLUS)) System.out.println ("Plus pressed") ;
	        if (be.wasReleased (WRButtonEvent.TWO)) System.out.println ("2 released") ;
	        if (be.wasReleased (WRButtonEvent.ONE)) System.out.println ("1 released") ;
	        if (be.wasReleased (WRButtonEvent.B)) System.out.println ("B released") ;
	        if (be.wasReleased (WRButtonEvent.A)) System.out.println ("A released") ;
	        if (be.wasReleased (WRButtonEvent.MINUS)) System.out.println ("Minus released") ;
	        if (be.wasReleased (WRButtonEvent.HOME)) System.out.println ("Home released") ;
	        
	        if (be.wasReleased (WRButtonEvent.LEFT)) {
	        	rotateInLocal(0, 1, 0);
	        }
	        if (be.wasReleased (WRButtonEvent.RIGHT)) {
	        	rotateInLocal(0, -1, 0);
	        }
	        if (be.wasReleased (WRButtonEvent.DOWN)) {
	        	translateInLocal(0, 0, 1);
	        }
	        if (be.wasReleased (WRButtonEvent.UP)) {
	        	translateInLocal(0, 0, -1);
	        }
	        
	        if (be.wasReleased (WRButtonEvent.PLUS)) System.out.println ("Plus released") ;
		}

		public void combinedInputReceived(WRCombinedEvent arg0) {
			
		}

		public void disconnected() {
			
		}

		public void extensionConnected(WiiRemoteExtension arg0) {
			try {
	            wiimote.setExtensionEnabled (true) ;
	         } catch (Exception e) {
	            e.printStackTrace();
	         }
		}

		public void extensionDisconnected(WiiRemoteExtension arg0) {
			
		}

		public void extensionInputReceived(WRExtensionEvent ee) {
			 if (ee instanceof WRNunchukExtensionEvent) {
		        WRNunchukExtensionEvent nee = (WRNunchukExtensionEvent)ee ;
		        
		        if (nee.wasReleased (WRNunchukExtensionEvent.C)) {
		        	buttonCPressed = false;
		        }
	            if (nee.wasReleased (WRNunchukExtensionEvent.Z)) {
	            	buttonZPressed = false;
	            }
	            if (nee.wasPressed (WRNunchukExtensionEvent.C)) {
	            	buttonCPressed = true;
	            }
	            if (nee.wasPressed (WRNunchukExtensionEvent.Z)) {
	            	buttonZPressed = true;
	            }
	            double x = nee.getAnalogStickData ().getX () ;
	            double y = nee.getAnalogStickData ().getY () ;
	            
	            Long currentTime = (new Date()).getTime();
	            
	            if(currentTime - precedentTime > 30) {
	            	if(x > 0.2) {
	            		if(buttonCPressed) {
	            			rotateInLocal(0, 0, -x/20);
	            		}
	            		else if(buttonZPressed){
	            			translateInLocal(x, 0, 0);
	            		}
	            		else {
	            			rotateInLocal(0, -x/20, 0);
	            		}
	            		precedentTime = currentTime;
	            	}
	            	if(x < -0.2) {
	            		if(buttonCPressed) {
	            			rotateInLocal(0, 0, -x/20);
	            		}
	            		else if(buttonZPressed){
	            			translateInLocal(x, 0, 0);
	            		}
	            		else {
	            			rotateInLocal(0, -x/20, 0);
	            		}
	            		precedentTime = currentTime;
	            	}
	            	if(y > 0.1) {
	            		if(buttonCPressed) {
	            			rotateInLocal(y/20, 0, 0);
	            		}
	            		else if(buttonZPressed){
	            			translateInLocal(0, y, 0);
	            		}
	            		else {
	            			translateInLocal(0, 0, -y);
	            		}
	            		precedentTime = currentTime;
	            	}
	            	if(y < -0.1) {
	            		if(buttonCPressed) {
	            			rotateInLocal(y/20, 0, 0);
	            		}
	            		else if(buttonZPressed){
	            			translateInLocal(0, y, 0);
	            		}
	            		else {
	            			translateInLocal(0, 0, -y);
	            		}
	            		precedentTime = currentTime;
	            	}
	            }
			 }
		}

		public void extensionPartiallyInserted() {
			
		}

		public void extensionUnknown() {
			
		}

		public void statusReported(WRStatusEvent arg0) {
			
		}
		
	}
}
